/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.dialog.students;

import com.toedter.calendar.JDateChooser;
import kz.kstu.ent.app.delegate.api.StudentDelegate;
import kz.kstu.ent.app.delegate.impl.StudentDelegateImpl;
import kz.kstu.ent.business.entity.Student;
import kz.kstu.ent.desktop.images.GUIFactory;
import kz.kstu.ent.desktop.images.GUIIcon;
import kz.kstu.ent.util.DateUtil;
import kz.kstu.ent.util.StringUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * Date: 17.09.12
 * Time: 21:55
 *
 * @author Oleg Anastassov
 */
public class EditStudentDialog extends JDialog {

    private static final String TITLE = "Список учеников :: Добавление нового ученика";

    private static final int WIDTH = 450;
    private static final int HEIGHT = 300;

    private JPanel rootPanel;
    private JTextField surnameTextField;
    private JTextField nameTextField;
    private JTextField patronymicTextField;
    private JCheckBox altynBelgiCheckBox;
    private JButton saveButton;
    private JButton cancelButton;
    private JDateChooser birthDateChooser;

    private Student student;

    public EditStudentDialog(Student student) {
        setIconImage(
                GUIFactory.getImage(GUIIcon.STUDENT_ADD)
        );
        this.student = student;
    }

    public void initStaticComponents() {
        setContentPane(rootPanel);
        setModal(true);
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setTitle(TITLE);

        initIcons();
        initListeners();
    }

    public void initData() {
        if (student != null) {
            String surname = student.getSurname();
            if (!StringUtil.isEmptyOrNullString(surname)) {
                surnameTextField.setText(surname);
            } else {
                surnameTextField.setText(StringUtil.EMPTY_STRING);
            }

            String name = student.getName();
            if (!StringUtil.isEmptyOrNullString(name)) {
                nameTextField.setText(name);
            } else {
                nameTextField.setText(StringUtil.EMPTY_STRING);
            }

            String patronymic = student.getPatronymic();
            if (!StringUtil.isEmptyOrNullString(patronymic)) {
                patronymicTextField.setText(patronymic);
            } else {
                patronymicTextField.setText(StringUtil.EMPTY_STRING);
            }

            Date birthDate = student.getBirthday();
            if (birthDate != null) {
                birthDateChooser.setDate(birthDate);
            } else {
                // nothing
            }

            Boolean isAltynBelgi = student.getAltynBelgi();
            if (isAltynBelgi != null) {
                altynBelgiCheckBox.setSelected(isAltynBelgi);
            } else {
                altynBelgiCheckBox.setSelected(false);
            }
        }
    }

    private void initListeners() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                save(getStudent());
            }
        });
    }

    private void initIcons() {
        saveButton.setIcon(
                GUIFactory.getIcon(GUIIcon.SAVE)
        );
        cancelButton.setIcon(
                GUIFactory.getIcon(GUIIcon.CANCEL)
        );
    }

    private void save(Student student) {
        StudentDelegate delegate = new StudentDelegateImpl();
        delegate.save(student);
    }

    private Student getStudent() {
        Student std = new Student();
        std.setSurname(surnameTextField.getText());
        std.setName(nameTextField.getText());
        std.setPatronymic(patronymicTextField.getText());
        std.setBirthday(
                DateUtil.toBeginOfDay(birthDateChooser.getDate())
        );
        std.setAltynBelgi(altynBelgiCheckBox.isSelected());

        Long id = student.getId();
        if (id != null) {
            std.setId(id);
        } else {
            std.setId(null);
        }
        return std;
    }

}
